/*
 * Licensed to the Apache Software Foudation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * � License �) ; you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * � AS IS � BASIS, WITHOUT WARRANTIES OR CONDITION OF ANY
 * KIND, either express or implied. See the License for the
 * specific langage governing permissions and limitations
 * under the License.
 *
 */
package views;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;


/**
 * 
 * A panel that contain a chart.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class CourbeJMXPanel extends JPanel
{
	private static final long serialVersionUID = 1L;
	
	/** The table that contains the informations. */
	private TimeSeries[] tabSeries;
	
	/** The panel that display the chart. */
	private ChartPanel chartPanel;
	
	/**
	 * 
	 * Creates a new instance of CourbeJMXPanel.
	 *
	 * @param nbCourbe The number of curve to display.
	 * @param title The title of the chart.
	 * @param xAxisLabel The label of the x axis.
	 * @param yAxisLabel The label of the y axis.
	 * @param minRange The bottom limit of the y axis.
	 * @param maxRange The upper limit of the y axis. if 0, let the library choose the range itself.
	 */
	public CourbeJMXPanel(int nbCourbe, String title, String xAxisLabel, String yAxisLabel, double minRange, double maxRange)
	{
		tabSeries = new TimeSeries[nbCourbe];
		final TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
		
		for(int i=0;i<nbCourbe;i++)
		{
			tabSeries[i] = new TimeSeries("Curve"+i);
			timeSeriesCollection.addSeries(tabSeries[i]);
		}
		
		final JFreeChart chart = ChartFactory.createTimeSeriesChart(title,xAxisLabel,yAxisLabel,timeSeriesCollection);
		
		/* configure the chart. */
		final XYPlot plot = chart.getXYPlot();
		ValueAxis axis = plot.getDomainAxis();
		axis.setAutoRange(true);
		axis.setFixedAutoRange(60000.0);
		if(maxRange !=0 )
		{
			axis = plot.getRangeAxis();
			axis.setRange(minRange, maxRange);
		}
		
		chartPanel = new ChartPanel(chart);
		this.setLayout(new BorderLayout(0,0));
		this.add(chartPanel,BorderLayout.CENTER);
	}
	
	/**
	 * 
	 * Add a value to the selected series.
	 *
	 * @param numSeries the number of the curve to update
	 * @param period the x value
	 * @param value the y value
	 */
	public void addValueToSeries(int numSeries, RegularTimePeriod period, double value)
	{
		tabSeries[numSeries].add(period, value);
	}
	
	/**
	 * 
	 * Set the title of the curve.
	 *
	 * @param numSeries the number of the curve to update
	 * @param title the title of the curve
	 */
	public void setSeriesTitle(int numSeries, String title)
	{
		tabSeries[numSeries].setKey(title);
	}
	
	
}
